home *** CD-ROM | disk | FTP | other *** search
/ BBS Toolkit / BBS Toolkit.iso / doors_1 / mach10.zip / BULLET.ZIP / BULLET12 < prev    next >
Text File  |  1987-08-09  |  20KB  |  444 lines

  1.  
  2.  
  3.  
  4.                                    MEGAlink
  5.                            A File Transfer Protocol
  6.  
  7.  
  8.                                 Specifications
  9.  
  10.  
  11.                                By: Paul Meiners
  12.  
  13.  
  14.                              P&M Software Company
  15.                              9350 Country Creek #30
  16.                              Houston, Texas 77036
  17.  
  18.                                 April 18, 1987
  19.  
  20.  
  21.                                Revision history:
  22.                                -----------------
  23.  
  24.                     August 9, 1987 ... Forsberg's CRC Variation
  25.  
  26.  
  27. Acknowledgements
  28. ----------------
  29. I would like to acknowledge the work of those who have done this before me.
  30. First, Chuck Forsberg, for his never ending quest for speed and reliability.
  31. Second, Ward Christensen, whose ideas are the genesis of this whole branch
  32. of file transfer technology.  Third, Peter Boswell, for showing me the way
  33. to a network friendly protocol.  Last, Tom Jennings, for the work he did
  34. on Fido and the Telink protocol, which introduced the "link" style header
  35. record, now in use by all "link" type protocols.
  36.  
  37. For those that are not aware, these pioneers are responsible for the following
  38. work:
  39.  
  40.    Ward Christensen ______________ Xmodem.
  41.    Tom Jennings __________________ Fido, Telink.
  42.    Chuck Forsberg ________________ Ymodem, Ymodem Batch, Zmodem.
  43.    Peter Boswell _________________ WXmodem.
  44.  
  45.  
  46.  
  47. Why Another Protocol?
  48. ---------------------
  49. In many respects, the design of a protocol has become a discussion between
  50. the above mentioned pioneers, each building on the work of the other.  Each
  51. has perceived a weakness in the protocols that have gone before and sought
  52. to improve the art.  And that is the most elegant reason for a new protocol,
  53. to advance the art.
  54.  
  55. I won't bother with a discussion of the merits of Xmodem.  Others have done
  56. that far better than I could.  Let me address the more modern variations.
  57. These are my opinions, based on experience and reading the literature.
  58.  
  59.    Zmodem  - The ideal protocol.  Highly reliable and fast.  Only failing
  60.              is that the cost of implementation is high.
  61.  
  62.    Ymodem  - Very fast, but not network friendly.  Also leaves something
  63.              to be desired on the reliability front.  Error correction is
  64.              slow.
  65.  
  66.    WXmodem - Fast and network friendly.  However, several questions exist
  67.              with regard to reliability.
  68.  
  69. So, it our goal to design and implement a protocol that meets or exceeds
  70. the following criteria:
  71.  
  72.    1.  Fast.  Efficency must exceed 95% on average basis.
  73.    2.  Reliable.  Known defects in prior protocols must be corrected.
  74.    3.  Inexpensive.  Implementation cost must be low.
  75.  
  76.  
  77.  
  78. How Do We Do It?
  79. ----------------
  80. Let us address these issues in reverse sequence.
  81.  
  82.     COST OF IMPLEMENTATION
  83.     ----------------------
  84.     By closely modeling the protocol after Xmodem, we hope to reduce cost
  85.     of implementation.  This may be a vain effort, because implementation
  86.     of any high performance protocol requires a certain degree of complexity.
  87.  
  88.     RELIABILITY
  89.     -----------
  90.     To improve the reliability of the protocol, we have choosen to use the
  91.     new CRC-32 technology.  The algorithm for this procedure will be given
  92.     later in this document.  Also, the Ack/Nak packets will be composed of
  93.     3 bytes, as was done in the SEAlink protocol.  Like WXmodem we will
  94.     honor all network XOFF signals at the transmitter end.
  95.  
  96.     SPEED
  97.     -----
  98.     Several things are going to be done to improve speed.  First, the block
  99.     size will be increased from 128 to 512 bytes.  This reduces the number
  100.     of envelope characters.  Like Zmodem, MEGAlink will be a full streaming
  101.     protocol, thus eliminating the turn-around time involved with Xmodem
  102.     and Ymodem.
  103.  
  104.  
  105.  
  106. Some definitions
  107. ----------------
  108.  
  109.      Description             Character           Decimal     Control Char
  110.      -----------             ---------           -------     ------------
  111.      XON ................... DC1 ............... 17 ............ ^Q
  112.      XOFF .................. DC3 ............... 19 ............ ^S
  113.      Acknowledge ........... ACK ............... 6 ............. ^F
  114.      Negative Acknowledge .. NAK ............... 21 ............ ^U
  115.      End of File ........... EOT ............... 4 ............. ^D
  116.      Start of Header ....... SOH ............... 1 ............. ^A
  117.      MegaLink Block Id ..... EM ................ 25 ............ ^Y
  118.      Request Status ........ RS ................ 30 ............ ^^
  119.      Cancel ................ CAN ............... 24 ............ ^X
  120.      Synchronize ........... SYN ............... 22 ............ ^V
  121.      Data Link Escape ...... DLE ............... 16 ............ ^P
  122.      CP/M EOF .............. SUB ............... 26 ............ ^Z
  123.  
  124.  
  125.  
  126. Receiver discipline
  127. -------------------
  128. The receive side of a MEGAlink transfer has three possible packets it can
  129. use.  Each is three characters in length as follows:
  130.  
  131.       Byte No.          Content
  132.       --------          -------
  133.          1              Ack/Nak/'C'.  No other character may appear.
  134.          2              Packet number.  0 thru 255.
  135.          3              Complement of packet number, i.e. (Pkt# XOR 255).
  136.  
  137. The 'C' packet is sent only at the start of each file transfer.  It is sent
  138. by the receiver at 5 second intervals, until the transmitter begins.  After
  139. that it is not used again until the next file is begun, or the next session
  140. is begun, if only 1 file is transmitted.
  141.  
  142. The Nak packet is used to request the retransmission of the specified packet.
  143.  
  144. The Ack packet is used to indicate the highest packet received without error.
  145.  
  146. Normally, the receiver remains quiet.  The only time packets are required of
  147. the receiver are at the beginning of the transfer, as outlined below, and upon
  148. receipt of an RS character, the Request Status character.  The receiver must
  149. respond to the RS character with an Ack packet, reflecting the highest block
  150. received in without error.  Of course, the receiver should send a Nak packet
  151. whenever a block is received with an error condition.
  152.  
  153. Note, that to maintain network friendliness, no packet may contain a XON or
  154. XOFF character.  These characters must be sent as two characters, first a
  155. DLE, followed by the folded XON or XOFF.  Characters are folded by XOR'ing
  156. them with decimal 64.  This scheme requires that the DLE character is
  157. escaped and folded in the same manner.
  158.  
  159.  
  160. Transmitter discipline
  161. ----------------------
  162. The transmitter can do four different things:
  163.  
  164.     1)  Send a header block.  Contains file name and other information
  165.         about the file.
  166.  
  167.     2)  Send a data block.  A data block contains 512 bytes of data.
  168.  
  169.     3)  Send an RS character, forces receiver to Ack the highest packet
  170.         he has received without error.
  171.  
  172.     4)  Send an EOT character, signals end-of-file to the receiver.
  173.  
  174.  
  175. Data blocks are sent without pause.  The transmitter should have enough
  176. buffers to cover the turn-around delay, so that any block the receiver
  177. may Nak will still be available in memory for retransmission.
  178.  
  179. Note, that to maintain network friendliness, no packet may contain a XON or
  180. XOFF character.  These characters must be sent as two characters, first a
  181. DLE, followed by the folded XON or XOFF.  Characters are folded by XOR'ing
  182. them with decimal 64.  This scheme requires that the DLE character is
  183. escaped and folded in the same manner.
  184.  
  185.  
  186. Typical transmit/receive dialog:
  187. --------------------------------
  188.  
  189. Transmitter                           Receiver     Description
  190. -----------                           --------     -----------
  191.                                       C   00 FF    opening Nak.
  192.  
  193. SOH 00 FF  header[128]  CRC CRC                    header block sent,
  194.                                                    using CRC-16.
  195.  
  196.                                       ACK 00 FF    Ack of header block.
  197.  
  198. EM  01 FE  data[512]  CRC CRC CRC CRC              file transmitted in one
  199. EM  02 FD  data[512]  CRC CRC CRC CRC              or more data blocks,
  200. EM  03 FC  data[512]  CRC CRC CRC CRC              using CRC-32.
  201. EM  04 FB  data[512]  CRC CRC CRC CRC
  202.  
  203. RS                                                 transmit requests status.
  204.  
  205.                                       ACK 04 FB    receive replies to RS.
  206.  
  207. EOT                                                end-of-file sent.
  208.  
  209.                                       ACK 04 FB    Ack sent for end-of-file.
  210.  
  211.                                       C   00 FF    opening Nak.
  212.  
  213. EOT                                                end-of-batch sent.
  214.  
  215.                                       ACK 04 FB    Ack sent for end-of-batch.
  216.  
  217.  
  218. If necessary, the last data block can be padded with CP/M EOF characters.
  219.  
  220. Note: all CRC bytes are transmitted from high to low order, NOT in the
  221.       more usual byte-reversed format.
  222.  
  223. At the end of the transaction, the "opening Nak" is repeated by the receiver.
  224. This is to allow for batch transmission, if more than one file were to be sent
  225. the transmitter would pick-up and start by sending the next header block and
  226. the transaction would continue from that point as shown.
  227.  
  228. The format of the header block conforms to the standard "link" format.
  229. Established by Tom Jennings when he designed the Telink protocol, also
  230. used by the SEAlink protocol.
  231.  
  232.      Byte Offset     Length       Content
  233.      -----------     ------       -------
  234.           0             4         Original file length. Integer in byte
  235.                                   reversed format.
  236.  
  237.           4             4         Date and time file was last mofified, in
  238.                                   seconds since 1979.  Same format DOS uses
  239.                                   in the directory entry.
  240.  
  241.           8            16         Original file name, as a null terminated
  242.                                   string.
  243.  
  244.          24             1         Binary version number.
  245.                                       0 ... Original MegaLink.
  246.                                       1 ... Forsberg's CRC Variation.
  247.  
  248.          25            15         Name of transmitting program, as a null
  249.                                   terminated string.
  250.  
  251.          40            88         Null filler and expansion area.
  252.  
  253.  
  254. CRC Calculator
  255. --------------
  256. The following routines demonstrate the technique used to calculate CRC's,
  257. both 16 & 32 bit varieties, used in MEGAlink.  The code is written in
  258. Turbo PASCAL.
  259.  
  260. Thanks to Mr. Chuck Forsberg, "Forsberg's CRC Variation" can be introduced.
  261. It involves initializing the CRC Register to $FFFFFFFF instead of the normal
  262. zero.  The use of this variation is signaled by a handshake between transmitter
  263. and receiver.  The receiver sends an opening nak packet containing a 'C' and 2
  264. bytes of 00 and FF hex.  If the receiver desires to use  "Forsberg's CRC
  265. Variation", he should code the bytes following the 'C' as 01 and FE.  If the
  266. transmitter is capable of using "Forsberg's CRC Variation" he will signal this
  267. in the header block, see offset 24 above.  The transmitter will adjust to use
  268. whichever CRC is requested by receiver, thus maintaining downward compatibility.
  269.  
  270.  
  271.    {  Global Variables  }
  272.  
  273.    TYPE
  274.       ARRAY512       = RECORD
  275.                           Len        : INTEGER;
  276.                           LongString : ARRAY[1..512] OF CHAR;
  277.                        END;
  278.       STRING128      = STRING[128];
  279.    VAR
  280.       crc_input        : INTEGER;      { 2 byte integer format }
  281.       crc_reg_lo       : INTEGER;
  282.       crc_reg_hi       : INTEGER;
  283.       forsberg_variant : BOOLEAN;
  284.  
  285.    PROCEDURE
  286.       ccitt_crc16_calc;                {  CRC-16  }
  287.    BEGIN
  288.       inline( $8B/$1E/crc_reg_hi );    {        mov     bx,crc_reg_hi    }
  289.       inline( $B9/>$08 );              {        mov     cx,8             }
  290.       inline( $A1/crc_input );         {        mov     ax,crc_input     }
  291.       inline( $D0/$D0 );               {  u1:   rcl     al,1             }
  292.       inline( $D1/$D3 );               {        rcl     bx,1             }
  293.       inline( $73/$04 );               {        jnc     u2               }
  294.       inline( $81/$F3/$1021 );         {        xor     bx,1021h         }
  295.       inline( $E2/$F4 );               {  u2:   loop    u1               }
  296.       inline( $89/$1E/crc_reg_hi );    {        mov     crc_reg_hi,bx    }
  297.    END;
  298.  
  299.    PROCEDURE
  300.       ccitt_crc32_calc;                {  CRC-32  }
  301.    BEGIN
  302.       inline( $8B/$1E/crc_reg_lo );    {       mov     bx,crc_reg_lo     }
  303.       inline( $8B/$16/crc_reg_hi );    {       mov     dx,crc_reg_hi     }
  304.       inline( $B9/>$08 );              {       mov     cx,8              }
  305.       inline( $A1/crc_input );         {       mov     ax,crc_input      }
  306.       inline( $D0/$D8 );               {  u1:  rcr     al,1              }
  307.       inline( $D1/$DA );               {       rcr     dx,1              }
  308.       inline( $D1/$DB );               {       rcr     bx,1              }
  309.       inline( $73/$08 );               {       jnc     u2                }
  310.       inline( $81/$F3/$8320 );         {       xor     bx,8320h          }
  311.       inline( $81/$F2/$EDB8 );         {       xor     dx,EDB8h          }
  312.       inline( $E2/$EE );               {  u2:  loop    u1                }
  313.       inline( $89/$1E/crc_reg_lo );    {       mov     crc_reg_lo,bx     }
  314.       inline( $89/$16/crc_reg_hi );    {       mov     crc_reg_hi,dx     }
  315.    END;
  316.  
  317.    PROCEDURE
  318.       calc_crc32(VAR cs : ARRAY512);
  319.    VAR
  320.       i  : INTEGER;
  321.    BEGIN
  322.    (*
  323.         Note: this routine calculates a 32 bit CRC based on the CCITT
  324.               polynomial.  The result is stored in the crc register,
  325.               variables crc_reg_hi & crc_reg_lo.
  326.    *)
  327.       IF (forsberg_variant) THEN BEGIN
  328.          crc_reg_hi:=$FFFF;
  329.          crc_reg_lo:=$FFFF;
  330.       END
  331.       ELSE BEGIN
  332.          crc_reg_hi:=0;
  333.          crc_reg_lo:=0;
  334.       END;
  335.       WITH cs DO BEGIN
  336.          FOR i:=1 TO Len DO BEGIN
  337.             crc_input:=ORD(LongString[i]);
  338.             ccitt_crc32_calc;
  339.          END;
  340.       END;
  341.       crc_input:=0;
  342.       ccitt_crc32_calc;
  343.       ccitt_crc32_calc;
  344.       ccitt_crc32_calc;
  345.       ccitt_crc32_calc;
  346.    END;
  347.  
  348.    PROCEDURE
  349.       calc_crc16(VAR cs : STRING128);
  350.    VAR
  351.       i  : INTEGER;
  352.    BEGIN
  353.    (*
  354.         Note: this routine calculates a 16 bit CRC based on the CCITT
  355.               polynomial.  The result is stored in the crc register,
  356.               variable crc_reg_hi.
  357.    *)
  358.       crc_reg_hi:=0;
  359.       crc_reg_lo:=0;
  360.       FOR i:=1 TO Length(cs) DO BEGIN
  361.          crc_input:=ORD(cs[i]);
  362.          ccitt_crc16_calc;
  363.       END;
  364.       crc_input:=0;
  365.       ccitt_crc16_calc;
  366.       ccitt_crc16_calc;
  367.    END;
  368.  
  369.  
  370.  
  371.  
  372. Buffer Management
  373. -----------------
  374. It is the responsibility of the transmitter to have enough buffer space
  375. to cover the Nak turn-around time at a particular baud rate.  Otherwise,
  376. in full stream mode, the receiver may Nak for a block that is not available.
  377. For example, at 2400 baud, assuming a turn-around delay of 6 seconds, the
  378. transmitter should have at least room for 4 blocks in his buffer area.  This
  379. in my opinion, would be cutting it TOO CLOSE, I would recommend a margin of
  380. at least 2 times or more.  In GT PowerComm, the first program to implement
  381. MEGAlink, we have a ring buffer of 32 blocks.  This is very easy to use,
  382. because the the Nak'ed block numbers can be AND'ed with $1F to produce the
  383. buffer number.  At 2400 baud, 32 blocks in the buffer amounts to more than
  384. 60 seconds.  This gives the receive side ample time to turn-around any Nak.
  385.  
  386. Notice that at higher baud rates, the time margin shrinks.  For example,
  387. at 9600 baud, the time margin for 32 blocks shrinks to about 14 seconds.
  388. This would probably be fine for a direct connect, but would not be good
  389. over a network such as PC Pursuit.  (This is not very important now, PC
  390. Pursuit is barely scratching the surface of 2400 baud at this time, 4/20/87.)
  391. At these higher baud rates, the transmitter must use flow control techniques to
  392. insure that an adequate time margin is maintained.  This can be done by issuing
  393. an RS, Request Status, command to the receive side.  The receiver should reply
  394. with an Ack packet indicating the last good packet received.  The transmitter
  395. should wait for reception of this Ack prior to continuing the dialog.  In
  396. effect this is a self-imposed flow control.  The transmitter must be smart
  397. enough to recognize the need for such, based on the baud rate and available
  398. buffer space.  Theoretically, if enough buffer space was available to the
  399. transmitter, it could continue to stream at any baud rate.
  400.  
  401.  
  402. Error Corrections Procedure
  403. ---------------------------
  404. When the receiver detects an error, it must immediately send a Nak packet
  405. with the offending packet # within.  Then the receiver dumps the contents
  406. of the serial port input buffer and waits for the requested packet.  The
  407. transmitter will not usually be able to respond immediately, so the receiver
  408. must expect packets with higher than expected packet #'s, until the requested
  409. packet arrives.  These extra packets should be disgarded without comment by
  410. receiver.
  411.  
  412. The transmitter, upon receipt of a Nak, will immediately dump the contents of
  413. the serial port output buffer and resend the requested packet.  At this point,
  414. due to the probability of a loss-of-sync error, which cannot be recovered, the
  415. send and receive side now drop to start-stop mode to correct the error.  In
  416. other words, the receiver must Ack a packet that he receives in good shape
  417. after a prior Nak has been sent for the packet.  The sender will not restart
  418. the stream of packets until this Ack has been received.  This prevents any
  419. chance for loss-of-sync at this juncture.  Also important to recognize is
  420. that the receive side of the transfer must have good code to find the start
  421. of a packet.  It will have a fairly unique signature, i.e. the character EM
  422. followed by two characters that are the complements of each other.  Especially
  423. considering the fact that the receiver is looking for a particular packet at
  424. this point, the occurence of a false signature is unlikely.
  425.  
  426. The question of how to handle packets arriving from the receiver that are them-
  427. selves in error has not been addressed.  It is true however, that the trans-
  428. mitter will respond to any Nak.  If the transmitter fails to get a good Nak
  429. the receiver will continue to Nak, until the requested block is received.
  430. This procedure works in practice, however there may well be a more elegant
  431. solution.  For example, the transmitter, upon receipt of a faulty Nak, could
  432. simply wait for the receiver to resend the Nak.  Of course, the transmitter
  433. could also send an RS, request status, command to the receiver, which could
  434. be interpreted in a Nakking situation as a "nak" to the "nak".  This should
  435. cause the receiver to resend the Nak.
  436.  
  437. GT implements the "resend-and-see" approach, this is the most direct and
  438. usually the best.  Even if the Nak is faulty, GT will choose from the 32
  439. buffers and send a block.  If the block is too high or too low, the receiver
  440. will Nak again.  This type of dialog usually leads to a resolution of the
  441. error condition, on all but the worst lines.  Naturally, even the best
  442. protocols fail on the worst lines!
  443.  
  444.